package com.zhangxiaofanfan.hotcode.second;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <a href="https://leetcode.cn/problems/partition-equal-subset-sum/?envType=study-plan-v2&envId=top-100-liked">416. 分割等和子集</a>
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode089 {
    public static void main(String[] args) {
        HotCode089 hotCode = new HotCode089();
        int[] nums = {1, 2, 5};
        System.out.println(hotCode.canPartition(nums));
    }

    public boolean canPartition(int[] nums) {
        if (nums == null || nums.length == 0) {
            throw new RuntimeException("入参函数不符合要求！");
        }
        int sum = Arrays.stream(nums).sum();
        if (sum % 2 == 1) {
            return false;
        }
        int half = sum >> 1;
        Arrays.sort(nums);
        boolean[] dp = new boolean[half + 1];
        dp[0] = true;
        for (int num : nums) {
            for (int i = half; i >= num; i--) {
                if (!dp[i] && dp[i - num]) {
                    dp[i] = true;
                }
            }
            if (dp[half]) {
                return true;
            }
        }
        return false;
    }
}